import java.util.Comparator;
import java.util.Timer;
import java.util.concurrent.PriorityBlockingQueue;

class MyTake  implements Comparable<MyTake>{
    private Runnable runnable;
    private long timer;

    public MyTake(Runnable runnable,long time) {
        this.runnable = runnable;
        this.timer = System.currentTimeMillis() + time;
    }

    public void run() {
        runnable.run();
    }

    public long getTimer() {
        return timer;
    }

    @Override
    public int compareTo(MyTake o) {
        return (int)(this.timer - o.timer);
    }
}


class MytTimer {

    PriorityBlockingQueue<MyTake> que = new PriorityBlockingQueue<>();

    public void schedule(Runnable runnable,long time) {
        MyTake myTake = new MyTake(runnable,time);
        que.put(myTake);
    }

    public MytTimer() {
        Thread t = new Thread(()-> {
            while(true) {
                try {
                    MyTake Mytake = que.take();
                    long curTime = System.currentTimeMillis();
                    if (Mytake.getTimer() > curTime) {
                        que.put(Mytake);
                    } else {
                        Mytake.run();
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        t.start();
    }
}
public class Main {
    public static void main(String[] args) {
        MytTimer mytTimer = new MytTimer();
        mytTimer.schedule(new Runnable() {
            @Override
            public void run() {
                System.out.println("111");
            }
        },1000);

        mytTimer.schedule(new Runnable() {
            @Override
            public void run() {
                System.out.println("222");
            }
        },3000);

        mytTimer.schedule(new Runnable() {
            @Override
            public void run() {
                System.out.println("333");
            }
        },2000);
    }
}
